using System;
using MusicStore.Dashboard.Client.SalesServiceReference;

namespace MusicStore.Dashboard.Client.Commands
{
  public class LoadSalesByMonthCommand : LoadSalesCommandBase<SalesByMonth>
  {
    protected override void Execute(SalesClient client)
    {
      Sales.Clear();

      client.GetSalesByMonthCompleted += ClientOnGetSalesByMonthCompleted;
      client.GetSalesByMonthAsync(this);
    }

    private void ClientOnGetSalesByMonthCompleted(object sender, GetSalesByMonthCompletedEventArgs args)
    {
      var wLoadSalesCommand = (SalesClient)sender;
      wLoadSalesCommand.GetSalesByMonthCompleted -= ClientOnGetSalesByMonthCompleted;

      var wSalesCommand = (LoadSalesByMonthCommand)args.UserState;
      wSalesCommand.Sales = args.Result;

      SetComplete();
    }
  }
}